package com.dao;

import com.helper.DBHelper;
import com.model.ScoreSet;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ScoreSetDao {

	private ScoreSet toScoreSet(Map<String, Object> data)
	{
		ScoreSet s = new ScoreSet();
		s.setId((Integer)data.get("id"));
		s.setPaperId((Integer)data.get("paper_id"));
		s.setChoiceScore((Float) data.get("choice_score"));
		s.setMultichoiceScore((Float)data.get("multichoice_score"));
		s.setJudgeScore((Float)data.get("judge_score"));
		s.setDiscussScore((Float)data.get("discuss_score"));
		return s;
	}

	public boolean delete(String id)
	{
		String sql = "DELETE FROM `score_set` where id=" + id ;
		DBHelper db = new DBHelper();
		return db.update(sql, null);
	}

	public Boolean addDefault() {
		String sql = "INSERT INTO `score_set`(`paper_id`, `choice_score`, `multichoice_score`, `judge_score`, `discuss_score`) VALUES (0, 2.0, 4.0, 2.0, 10.0);\n";
		DBHelper db = new DBHelper();
		return db.update(sql, null);
	}

	public boolean updateScore(int setId, float c, float m, float j, float d) {
		int pid = getById(setId).getPaperId();
		return updateChoice(pid, c) && updateMultichoice(pid, m) && updateJudge(pid, j) && updateDiscuss(pid, d);
	}

	public boolean updateChoice(int paperId, float score) {
		String sql = "UPDATE `score_set` SET `choice_score` = " + score + " WHERE `paper_id` = " + paperId;
		DBHelper db = new DBHelper();
		return db.update(sql, null);
	}

	public boolean updateMultichoice(int paperId, float score) {
		String sql = "UPDATE `score_set` SET `multichoice_score` = " + score + " WHERE `paper_id` = " + paperId;
		DBHelper db = new DBHelper();
		return db.update(sql, null);
	}

	public boolean updateJudge(int paperId, float score) {
		String sql = "UPDATE `score_set` SET `judge_score` = " + score + " WHERE `paper_id` = " + paperId;
		DBHelper db = new DBHelper();
		return db.update(sql, null);
	}

	public boolean updateDiscuss(int paperId, float score) {
		String sql = "UPDATE `score_set` SET `discuss_score` = " + score + " WHERE `paper_id` = " + paperId;
		DBHelper db = new DBHelper();
		return db.update(sql, null);
	}
	
	public ScoreSet getByPaperId(int pid) {
		ScoreSet result = new ScoreSet();
		String sql = "select * from `score_set` where paper_id=" + pid;
		DBHelper db = new DBHelper();
		List<Map<String, Object>> data = db.query(sql, null);
		for(Map<String, Object> ele : data) {
			result = toScoreSet(ele);
		}
		return result;
	}

	public ScoreSet getById(int pid) {
		ScoreSet result = new ScoreSet();
		String sql = "select * from `score_set` where id=" + pid;
		DBHelper db = new DBHelper();
		List<Map<String, Object>> data = db.query(sql, null);
		for(Map<String, Object> ele : data) {
			result = toScoreSet(ele);
		}
		return result;
	}

	public List<ScoreSet> getAll() {
		List<ScoreSet> result = new ArrayList<>();
		String sql = "select * from `score_set`";
		DBHelper db = new DBHelper();
		List<Map<String, Object>> data = db.query(sql, null);
		for(Map<String, Object> ele : data) {
			result.add(toScoreSet(ele));
		}
		return result;
	}


	public boolean setToPaper(String setId, String paperId) {
		String sql = "UPDATE `score_set` SET `paper_id` = " + paperId + " WHERE `id` = " + setId;
		DBHelper db = new DBHelper();
		return db.update(sql, null);
	}
}
